#coding: utf-8
"""
    file name: VoiceConsole.py
    platform: Windows,Linux
    description: 机器人问答控制台

    module author: Guangwei Jiang (Guangwei_Jiang@wistron.com)
    Create Date: 2020/9/20

"""
import BaiduVoice
import AdditionSubtraction
import TulingRobot
import pyttsx3

class VoiceConsole:
    def __init__(self):
        try:
            self.baiduVoice = BaiduVoice.BaiduVoice()
            self.tuling = TulingRobot.TulingRobot()
        except Exception as e:
            print(e)
            
    def RobotVoice(self, robotsay="没听清，请再说一遍"):
        """Function to make speech from text

        |  Args:
        |  1. robotsay: the text need convert to speech

        |  Return -- NA

        >>> # make speech from text
        >>> RobotVoice("你好啊！")
        """
        try:
            engine=pyttsx3.init()
            rate=engine.getProperty('rate')
            engine.setProperty('rate',rate-66)
            engine.say(robotsay)
            engine.runAndWait()
            
        except Exception as e:
            print(e)
            
                    
    def startService(self):
        """Function to start voice console service

        |  Args -- NA

        |  Return -- NA

        >>> # start voice console service
        >>> startService()
        """
        try:
            while(True):
                strInput = self.baiduVoice.speechToText()
                print(strInput)
                if "加减法" in strInput:
                    while(True):
                        self.RobotVoice("选择键盘输入还是麦克风？")
                        strInput = self.baiduVoice.speechToText()
                        if "键盘" in strInput:
                            AdditionSubtraction.startTest(inputMethod="Keypad")
                            break
                        elif "麦克风" in strInput:
                            AdditionSubtraction.startTest(inputMethod="Speech")
                            break
                        else:
                            self.RobotVoice("没有听清楚，请再说一遍。")
                elif "小叮当" in strInput:
                    response = self.tuling.response(strInput)
                    print(response)
                    self.RobotVoice(response)
        except Exception as e:
            print(e)

if __name__ == "__main__":
    voiceConsole = VoiceConsole()
    voiceConsole.startService()
